
cap log close
log using "$logs/seda-gaps.log", replace
	
	use "$data/sedagaps", clear
	
/********************************************************************************
	Merge data
********************************************************************/
	
	// Merge with sample 
	merge 1:1 countyid using "$data/commonsamp" 
	tab _merge // 76% of counties
	keep if _merge == 3
	drop _merge
	
	// Merge with covariates
	merge 1:1 countyid using "$data/seda_covariates_clean", assert(2 3) nogen keep(3)
	
	// Merge with bias
	merge 1:1 countyid using "$data/mrp_data", assert(2 3) nogen keep(3)
	
/********************************************************************************
	analysis using my aggregated/weighted covariates
********************************************************************/
	
preserve

	// Standardize bias scores
	foreach var in all_imp_bias all_exp_bias tch_imp_bias tch_exp_bias {
		egen temp = std(`var')
		replace `var' = temp
		drop temp
		}
		
	// Generate a state variable
	tab fips, gen(state_)
	
	est clear

	// Pooled
	eststo: metareg mn_avg_ol all_imp_bias state_*, wsse(mn_avg_ol_se) mm
	eststo: metareg mn_avg_ol all_imp_bias sesall ///
		perblk perhsp /*pctenglish1*/ hswhtblk hsflnfl percharter_all state_*, wsse(mn_avg_ol_se) mm // SES+ Descriptives
	eststo: metareg mn_avg_ol all_imp_bias sesall ///
		perblk perhsp /*pctenglish1*/ hswhtblk hsflnfl percharter_all ///
		ppexp_inst stutch_all state_*, wsse(mn_avg_ol_se) mm // SES+Desriptives+Instruction
	eststo: metareg mn_avg_ol all_imp_bias sesall ///
		perblk perhsp /*pctenglish1*/ hswhtblk hsflnfl percharter_all ///
		ppexp_inst stutch_all ///
		flunchwhtblk percharterwhtblk stutchwhtblk seswhtblk ///
		state_*, wsse(mn_avg_ol_se) mm // SES+Desriptives+Instruction+Gaps

	eststo: metareg mn_avg_ol all_exp_bias state_*, wsse(mn_avg_ol_se) mm
	eststo: metareg mn_avg_ol all_exp_bias sesall ///
		perblk perhsp /*pctenglish1*/ hswhtblk hsflnfl percharter_all state_*, wsse(mn_avg_ol_se) mm // SES+ Descriptives
	eststo: metareg mn_avg_ol all_exp_bias sesall ///
		perblk perhsp /*pctenglish1*/ hswhtblk hsflnfl percharter_all ///
		ppexp_inst stutch_all state_*, wsse(mn_avg_ol_se) mm // SES+Desriptives+Instruction
	eststo: metareg mn_avg_ol all_exp_bias sesall ///
		perblk perhsp /*pctenglish1*/ hswhtblk hsflnfl percharter_all ///
		ppexp_inst stutch_all ///
		flunchwhtblk percharterwhtblk stutchwhtblk seswhtblk ///
		state_*, wsse(mn_avg_ol_se) mm // SES+Desriptives+Instruction+Gaps
		
	esttab using "$output/all-seda-gaps-full.csv", se r2 ar2 drop(state*) title(pooled bias) replace note(includes state FE) ///
		star(~ 0.10 * 0.05 ** 0.01 *** 0.001)
	esttab using "$output/all-seda-gaps-simple.csv", se r2 ar2 keep(all_???_bias) title(pooled bias) replace note(includes state FE) ///
		star(~ 0.10 * 0.05 ** 0.01 *** 0.001)

restore

// Teachers

	keep if tchsamp
	gen origtchimpbias=tch_imp_bias

	// Standardize bias scores
		foreach var in all_imp_bias all_exp_bias tch_imp_bias tch_exp_bias {
			egen temp = std(`var')
			replace `var' = temp
			drop temp
			}
		
	// Generate a state variable
	tab fips, gen(state_)
	est clear
	
	eststo: metareg mn_avg_ol all_imp_bias ///
		state_*, wsse(mn_avg_ol_se) mm // All 
	eststo: metareg mn_avg_ol all_imp_bias ///
		sesall ///
		perblk perhsp /*pctenglish1*/ hswhtblk hsflnfl percharter_all ///
		ppexp_inst stutch_all ///
		flunchwhtblk percharterwhtblk stutchwhtblk seswhtblk ///
		state_*, wsse(mn_avg_ol_se) mm // All
		
	eststo: metareg mn_avg_ol tch_imp_bias state_*, wsse(mn_avg_ol_se) mm
	eststo: metareg mn_avg_ol tch_imp_bias sesall ///
		perblk perhsp /*pctenglish1*/ hswhtblk hsflnfl percharter_all state_*, wsse(mn_avg_ol_se) mm // SES+ Descriptives
	eststo: metareg mn_avg_ol tch_imp_bias sesall ///
		perblk perhsp /*pctenglish1*/ hswhtblk hsflnfl percharter_all ///
		ppexp_inst stutch_all state_*, wsse(mn_avg_ol_se) mm // SES+Desriptives+Instruction
	eststo: metareg mn_avg_ol tch_imp_bias sesall ///
		perblk perhsp /*pctenglish1*/ hswhtblk hsflnfl percharter_all ///
		ppexp_inst stutch_all ///
		flunchwhtblk percharterwhtblk stutchwhtblk seswhtblk ///
		state_*, wsse(mn_avg_ol_se) mm // SES+Desriptives+Instruction+Gaps
				
	eststo: metareg mn_avg_ol all_exp_bias state_*, wsse(mn_avg_ol_se) mm
	eststo: metareg mn_avg_ol all_exp_bias ///
		sesall ///
		perblk perhsp /*pctenglish1*/ hswhtblk hsflnfl percharter_all ///
		ppexp_inst stutch_all ///
		flunchwhtblk percharterwhtblk stutchwhtblk seswhtblk ///
		state_*, wsse(mn_avg_ol_se) mm // All
		
	eststo: metareg mn_avg_ol tch_exp_bias state_*, wsse(mn_avg_ol_se) mm
	eststo: metareg mn_avg_ol tch_exp_bias sesall ///
		perblk perhsp /*pctenglish1*/ hswhtblk hsflnfl percharter_all state_*, wsse(mn_avg_ol_se) mm // SES+ Descriptives
	eststo: metareg mn_avg_ol tch_exp_bias sesall ///
		perblk perhsp /*pctenglish1*/ hswhtblk hsflnfl percharter_all ///
		ppexp_inst stutch_all state_*, wsse(mn_avg_ol_se) mm // SES+Desriptives+Instruction
	eststo: metareg mn_avg_ol tch_exp_bias sesall ///
		perblk perhsp /*pctenglish1*/ hswhtblk hsflnfl percharter_all ///
		ppexp_inst stutch_all ///
		flunchwhtblk percharterwhtblk stutchwhtblk seswhtblk ///
		state_*, wsse(mn_avg_ol_se) mm // SES+Desriptives+Instruction+Gaps
		
	esttab using "$output/tch-seda-gaps-full.csv", se r2 ar2 drop(state*) title(pooled bias) replace note(includes state FE) ///
		star(~ 0.10 * 0.05 ** 0.01 *** 0.001)
	esttab using "$output/tch-seda-gaps-simple.csv", se r2 ar2 keep(???_???_bias) title(pooled bias) replace note(includes state FE) ///
		star(~ 0.10 * 0.05 ** 0.01 *** 0.001)
	
	// Margins plot
	preserve
		use "$raw/seda/SEDA_county_pool_gcs_v30.dta", clear
		keep if subgroup == "wbg"
		keep countyid countyname fips stateabb mn_avg_ol mn_avg_ol_se
		egen rm = rowmiss(*)
		assert rm==2|rm==0
		keep if rm == 0
		drop rm
		isid countyid fips 
		destring countyid, replace
		rename mn_avg_ol gcs
		rename mn_avg_ol_se gcs_se
		tempfile gcs
		save `gcs'
	restore
	merge 1:1 countyid using `gcs', keepusing(gcs*) assert(2 3) keep(3) nogen
	
	metareg gcs origtchimpbias sesall ///
		perblk perhsp /*pctenglish1*/ hswhtblk hsflnfl percharter_all ///
		ppexp_inst stutch_all ///
		flunchwhtblk percharterwhtblk stutchwhtblk seswhtblk ///
		state_*, wsse(gcs_se) mm // SES+Desriptives+Instruction+Gaps

	metareg mn_avg_ol origtchimpbias sesall ///
		perblk perhsp /*pctenglish1*/ hswhtblk hsflnfl percharter_all ///
		ppexp_inst stutch_all ///
		flunchwhtblk percharterwhtblk stutchwhtblk seswhtblk ///
		state_*, wsse(mn_avg_ol_se) mm // SES+Desriptives+Instruction+Gaps
		
		mgen, at(origtchimpbias=(.05(.05).45)) stub(xb)
		
		cap drop tag
		egen tag = tag(countyid)
		
		sum origtchimpbias if tag
		local mean=r(mean)

		twoway (connected xbxb xborigtchimpbias, mcolor(black) lcolor(black) xline(`mean', lcolor(black))) ///
			(rarea xbll xbul xborigtchimpbias, color(black%30) xline(.15, lcolor(black) lpattern(dash))), ///
			legend(order(1 "White-Black" 2 "95%CI")) ytitle(White-Black Difference in Test Scores) xtitle(Teacher Implicit Bias) ///
				yscale(range(.25 .75)) ylabel(.25(.1).75) xscale(range(0 .5)) xlabel(0(.1).5)


	
log close
